package game;

import javax.swing.JOptionPane;

import boundaryToMatador.GUI;

public class ChanceCard {

	private String 		text;
	private String 		type;
	private int			value;
	private boolean 	showPopups = true;

	public ChanceCard(String cardType, String cardText, int cardValue)
	{
		type	= cardType;
		text	= cardText;
		value	= cardValue;
	}

	public void setShowPopups(boolean state)
	{
		showPopups = state;
	}

	// Udfører en handling baseret på typen af kort.
	public void action(Player player) 
	{
		int positionBefore;

		if (showPopups)
		{
			JOptionPane.showMessageDialog(null, "Prøv Lykken\n"+ text);
		}
		switch (type)
		{
		// Flytter spilleren hen på kortets værdi
		case "Move":
			positionBefore = player.getPosition();
			if(value == 1){
				player.setPosition(1);
			}
			else
			{
				player.setPosition(value);
				if ((!showPopups) && (Board.fields[value-1] instanceof Ownable)) {
					((Ownable) Board.fields[value-1]).setForceToBuy(true);
				}
				Board.fields[value-1].landOnField(player);
			}

			GUI.removeCar(positionBefore, player.getName());
			GUI.setCar(player.getPosition(), player.getName());
			break;
			// Flytter spilleren til næste rederi
		case "MoveToShip":
			int[]	shipPositions = new int[] {5,15,25,35};
			int		moveTo = 0;

			positionBefore = player.getPosition();

			for(int i = 0; i < shipPositions.length; i++)
			{
				if((player.getPosition() >= shipPositions[i]) && ((player.getPosition() < shipPositions[3])))
				{
					moveTo = shipPositions[i+1];
				}
				else
				{
					if((player.getPosition() >= shipPositions[3]) || (player.getPosition() < shipPositions[0]))
					{
						moveTo = shipPositions[0];
					}
				}
			}
			player.setPosition(moveTo+1);

			GUI.removeCar(positionBefore, player.getName());
			GUI.setCar(player.getPosition(), player.getName());
			if ((!showPopups) && (Board.fields[moveTo] instanceof Ownable)) {
				((Ownable)Board.fields[moveTo]).setForceToBuy(true);
			}
			Board.fields[moveTo].landOnField(player);
			break;
			//Flytter spilleren i fængsel
		case "MoveToJail":
			positionBefore = player.getPosition();

			player.setPosition(31);

			GUI.removeCar(positionBefore, player.getName());
			GUI.setCar(player.getPosition(), player.getName());

			Board.fields[30].landOnField(player);
			break;
		case "Receive":
			player.getAccount().addPoints(value);
			break;
		case "Pay":
			player.getAccount().addPoints(-value);
			break;
			//			case "Pardon":
			/* Kortet kan beholdes
			 * Kortet kan sælges frit
			 * Kortet kan indløses med det samme, hvis man ryger i fængsel. Derpå man kan rykke næste tur.
			 */
			//			break;
			//Giver spilleren 200 kr. fra hver af de andre spillere, som tages fra deres konto.
			//			case "ReceiveFromAll":
			//birthdayPresent = value*[Game.numOfPlayers-1];
			//player.getAccount().addPoints(birthdayPresent);
			/*Kortet tager 200 fra hver spiller undtagen korttageren og giver dem til korttageren
			 */
			//			break;
			//Pålægger en skat på huse og hoteller, som skal betaler af spilleren, såfremt der ejes sådan nogle.
		case "RememberTax1":
			int houseTax = 0;
			int housePrice = 500;
			int hotelPrice = 2000;
			for(int i = 0; i < Board.fields.length; i++)
			{
				if(Board.fields[i] instanceof Street)
				{
					Street fieldAsStreet = (Street) Board.fields[i];
					if(fieldAsStreet.getOwner() == player)
					{
						if(fieldAsStreet.getProperty() == 5)
						{
							houseTax = houseTax + hotelPrice;
						}
						else
						{
							houseTax = houseTax + (housePrice*fieldAsStreet.getProperty());
						}
					}
				}
			}
			player.getAccount().addPoints(-houseTax);
			break;
			//Samme som ovenstående, men med andre variabler
		case "RememberTax2":
			int houseTax2		= 0;
			int housePrice2	= 800;
			int hotelPrice2	= 2300;
			for(int i = 0; i < Board.fields.length; i++)
			{
				if(Board.fields[i] instanceof Street)
				{
					Street fieldAsStreet = (Street) Board.fields[i];
					if(fieldAsStreet.getOwner() == player)
					{
						if(fieldAsStreet.getProperty() == 5)
						{
							houseTax2 = houseTax2 + hotelPrice2;
						}
						else
						{
							houseTax2 = houseTax2 + (housePrice2*fieldAsStreet.getProperty());
						}
					}
				}
			}
			player.getAccount().addPoints(-houseTax2);
			break;
			//Matadorlegatet. Såfremt spilleren har under 15000 i samlede værdier, gives 40000 til spilleren.
		case "Grant":
			int legatSum				= 40000;
			int valueOfProperties	= 0;
			int valueOfOwnables		= 0;
			int accountStatus			= player.getAccount().getPoint();

			//Værdi af spillerens huse på grunde
			for(int i = 0; i < Board.fields.length; i++)
			{
				if(Board.fields[i] instanceof Street)
				{
					Street fieldAsStreet = (Street) Board.fields[i];
					if(fieldAsStreet.getOwner() == player)
					{	
						valueOfProperties = valueOfProperties + (fieldAsStreet.getPropertyPrice()*fieldAsStreet.getProperty());
					}
				}
			}

			//Værdi af spillerens grunde
			for(int i = 0; i < Board.fields.length; i++)
			{
				if(Board.fields[i] instanceof Ownable)
				{
					Ownable fieldAsOwnable = (Ownable) Board.fields[i];
					if(fieldAsOwnable.getOwner() == player)
					{
						valueOfOwnables = valueOfOwnables + fieldAsOwnable.getPrice();
					}
				}
			}
			//validsum = pengebeholdning+huse+skøder
			int validSum = accountStatus + valueOfProperties + valueOfOwnables;
			if(validSum <= value)
			{
				player.getAccount().addPoints(legatSum);
			}
			break;
			//Flytter spilleren til næste rederi, og pålægger ham at betale dobbelt leje, hvis feltet er ejet.
		case "MoveToShipDoubleRent":
			int[] shipPositions1 = new int[]{5,15,25,35};
			int moveTo1 = 0;
			positionBefore = player.getPosition();

			for(int i = 0; i < shipPositions1.length; i++)
			{
				if((player.getPosition() >= shipPositions1[i]) && ((player.getPosition() < shipPositions1[3])))
				{
					moveTo1 = shipPositions1[i+1];
				}
				else
				{
					if((player.getPosition() >= shipPositions1[3]) || (player.getPosition() < shipPositions1[0]))
					{
						moveTo1 = shipPositions1[0];
					}
				}
			}
			player.setPosition(moveTo1+1);

			GUI.removeCar(positionBefore, player.getName());
			GUI.setCar(player.getPosition(), player.getName());

			if ((!showPopups) && (Board.fields[moveTo1] instanceof Ownable)) {
				((Ownable)Board.fields[moveTo1]).setForceToBuy(true);
			}
			((Ship) Board.fields[moveTo1]).setDoubleRent(true);
			Board.fields[moveTo1].landOnField(player);
			break;
			//Flytter spilleren tilbage, svarende til kortets værdi.
		case "MoveBack":
			positionBefore = player.getPosition();
			if (player.getPosition() == 3){

				player.setPosition(40);

				GUI.removeCar(positionBefore, player.getName());
				GUI.setCar(player.getPosition(), player.getName());

				if ((!showPopups) && (Board.fields[39] instanceof Ownable)) {
					((Ownable)Board.fields[39]).setForceToBuy(true);
				}
				Board.fields[39].landOnField(player);
			} else {
				player.setPosition(player.getPosition() + value);
				GUI.removeCar(positionBefore, player.getName());
				GUI.setCar(player.getPosition(), player.getName());
				if ((!showPopups) && (Board.fields[player.getPosition() + value] instanceof Ownable)) {
					((Ownable)Board.fields[player.getPosition() + value]).setForceToBuy(true);
				}
				Board.fields[player.getPosition() + value].landOnField(player);
			}

			break;
		}
	}

	public String getText()
	{
		return text;
	}
	public String getType()
	{
		return type;
	}
	public int getValue(){
		return value;
	}
}